package cn.lanqiao.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.User;
import cn.lanqiao.util.JdbcUtil;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class FriendFrm extends JFrame {

	private JPanel contentPane;
	private JTextField idField;
	private JTable Jtable;
	private static User user;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FriendFrm frame = new FriendFrm(user);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public FriendFrm(User user) {
		this.user = user;
		setResizable(false);
		setTitle("好友表");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel labsearch = new JLabel("请输入要查询的ID:");
		labsearch.setFont(new Font("宋体", Font.PLAIN, 14));
		labsearch.setBounds(24, 23, 139, 21);
		contentPane.add(labsearch);
		
		idField = new JTextField();
		idField.setBounds(146, 23, 161, 21);
		contentPane.add(idField);
		idField.setColumns(10);
		JButton bntSearch = new JButton("查询");
		//给查询添加监听器
		bntSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//取出好友id,与数据库好友id比对
				String id = idField.getText();
				//验证id是否存在
				cn.lanqiao.service.loadFriend service = new cn.lanqiao.serviceimpl.isScuccessImpl();
				try {
					boolean isOk = service.isSuccess(id);
					if(isOk) {
						//实现跳转进入所查询人的界面
						FriendInforFrm friendInfoFrm = new FriendInforFrm();
						friendInfoFrm.setVisible(true);
					}else {
						JOptionPane.showMessageDialog(null, "查无此人");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		bntSearch.setFont(new Font("宋体", Font.PLAIN, 14));
		bntSearch.setBounds(317, 22, 67, 23);
		contentPane.add(bntSearch);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportBorder(null);
		scrollPane.setBounds(24, 76, 359, 151);
		contentPane.add(scrollPane);
		//初始化jtable
		Jtable = new JTable();
		//调业务层
		cn.lanqiao.service.showFriendName service  = new cn.lanqiao.serviceimpl.showFriendNameImpl();
		try {
			//将数据库中的好友表名字显示在表中
			Object[][] datas = service.data(user.getUserID());
			String[] head = new String[] {"好友ID"};
			Jtable.setModel(new DefaultTableModel(datas,head));
			Jtable.setEnabled(false);
			scrollPane.setViewportView(Jtable);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		scrollPane.setViewportView(Jtable);
		setLocationRelativeTo(null);		
	}
}
